package com.example.gpio_app;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.google.android.material.textfield.TextInputEditText;

public class GpioViewAdapter extends BaseAdapter {
    private final Context context;
    private final List<Map<String, Object>> productData;
    private final LayoutInflater inflater;

    private final Gpio_Jni gpioJni;

    char gpio_out_port;
    int gpio_out_num;
    char gpio_in_port;
    int gpio_in_num;


    //初始化
    public GpioViewAdapter(Context context, List<Map<String, Object>> productData) {
        this.context = context;
        this.productData = productData;
        this.inflater = LayoutInflater.from(context);
        gpioJni = new Gpio_Jni();
    }

    @Override
    public int getCount() {
        //获取数量
        return productData.size();
    }

    @Override
    public Object getItem(int position) {
        //获取对象
        return productData.get(position);
    }

    @Override
    public long getItemId(int position) {
        //获取Item模板
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //position是选中的ViewList中list的列中的索引
        //convertView是用于展示在页面中空白item，最后需要返回一个已填充的convertView返回
        //定义一个ViewHolder类
        ViewHolder holder;
        //根据点击获取的list索引寻找对应的Map
        Map<String, Object> product = productData.get(position);

        if (convertView == null) {
            //根据获取的item创建一个视图
            convertView = inflater.inflate(R.layout.activity_gpio_view, parent, false);
            //绑定item中的控件到ViewHolder身上
            holder = new ViewHolder();
            holder.title = convertView.findViewById(R.id.title);
            holder.gpioPost = convertView.findViewById(R.id.gpioPort);
            holder.gpioNum = convertView.findViewById(R.id.gpioNum);
            holder.highCheck = convertView.findViewById(R.id.highBtn);
            holder.lowCheck = convertView.findViewById(R.id.lowBtn);
            holder.startBtn = convertView.findViewById(R.id.startBtn);
            holder.radioGroup = convertView.findViewById(R.id.radioGroup);
            holder.gpioValTxt = convertView.findViewById(R.id.gpioValTxt);

            String titleName = (String) product.get("name");

            if(Objects.equals(titleName, "OutPut")){
                holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup radioGroup, int checkedRadioButtonId) {
                        Log.e(this.getClass().getName(), "----------------------"+ gpio_out_port + " : " + gpio_out_num +"---------------------------");
                        Log.e(this.getClass().getName(), "----------------------"+ gpio_in_port + " : " + gpio_in_num +"---------------------------");
                        if(checkedRadioButtonId == holder.highCheck.getId()){
                            //write gpio
                            gpioJni.gpioOutput(gpio_out_port,gpio_out_num,1);
                            //read gpio
                            int gpioVal = gpioJni.gpioInput(gpio_in_port, gpio_in_num);
                            if(gpioVal == 1){
                                Toast.makeText(context, "read gpio p" + gpio_out_port + gpio_out_num + "  value is high", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(context, "testing error", Toast.LENGTH_SHORT).show();
                            }
                        }else if(checkedRadioButtonId == holder.lowCheck.getId()){
                            //write gpio
                            gpioJni.gpioOutput(gpio_out_port,gpio_out_num,0);
                            //read gpio
                            int gpioVal = gpioJni.gpioInput(gpio_in_port, gpio_in_num);
                            if(gpioVal == 0){
                                Toast.makeText(context, "read gpio p" + gpio_out_port + gpio_out_num + "  value is low", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(context, "testing error", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
            }

            holder.startBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.startBtn.setVisibility(INVISIBLE);
                   if(Objects.equals(titleName, "OutPut")){
                       holder.radioGroup.setVisibility(VISIBLE);
                       holder.gpioValTxt.setVisibility(VISIBLE);
                       gpio_out_port = Objects.requireNonNull(holder.gpioPost.getText()).charAt(0);
                       gpio_out_num = Integer.parseInt(Objects.requireNonNull(holder.gpioNum.getText()).toString());
                   }else if(Objects.equals(titleName, "InPut")){
                       gpio_in_port = Objects.requireNonNull(holder.gpioPost.getText()).charAt(0);
                       gpio_in_num = Integer.parseInt(Objects.requireNonNull(holder.gpioNum.getText()).toString());
                   }

                }
            });

            convertView.setTag(holder);
        } else {
            //如果已经存在视图
            holder = (ViewHolder) convertView.getTag();
        }

        //根据Map中的信息，以键找值的形式一一赋值給每一个控件，以做到对控件的数据的填充
        holder.title.setText((String)product.get("name"));
        return convertView;
    }


    //定义ViewHolder，将item中所有的控件都列一个变量
    private static class ViewHolder {
        TextView title;
        TextInputEditText gpioPost;
        TextInputEditText gpioNum;
        RadioButton highCheck;
        RadioButton lowCheck;
        Button startBtn;
        TextView gpioValTxt;
        RadioGroup radioGroup;

    }
}